package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.api.dto.CoachCommentUser;
import com.maiworld.api.vo.ResultVO;
import com.maiworld.mapper.TbCoachMapper;
import com.maiworld.mapper.TbEvaluationMapper;
import com.maiworld.mapper.TbReleaseNewsMapper;
import com.maiworld.mapper.TbReservationMapper;
import com.maiworld.mapper.TbStudentMapper;
import com.maiworld.pojo.ReleConseReserStud;
import com.maiworld.pojo.TbCoach;
import com.maiworld.pojo.TbCoachExample;
import com.maiworld.pojo.TbCoachExample.Criteria;
import com.maiworld.pojo.TbReservation;
import com.maiworld.seller.service.CoachService;

import entity.PageResult;

@Service
public class CoachServiceImpl implements CoachService {

	@Autowired
	private TbCoachMapper tbCoachMapper;
	
	@Autowired
	private TbEvaluationMapper tbEvaluationMapper;
	
	@Autowired
	private TbStudentMapper tbStudentMapper;
	
	@Autowired
	private TbReservationMapper tbReservationMapper;
	
	@Autowired
	private TbReleaseNewsMapper tbReleaseNewsMapper;
	@Override
	public List<TbCoach> findAll() {
		
		return tbCoachMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {		
		PageHelper.startPage(pageNum, pageSize);
		Page<TbCoach> page = (Page<TbCoach>) tbCoachMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
	@Override
	public void add(TbCoach coach) {
		coach.setAuthentication("1");
		coach.setAuthentication("0");	//状态
		coach.setCreated(new Date());   //创建时间
		
		tbCoachMapper.insert(coach);

	}

	@Override
	public void update(TbCoach coach) {
		tbCoachMapper.updateByPrimaryKey(coach);

	}

	@Override
	public TbCoach findOne(Long cid) {
		return tbCoachMapper.selectByPrimaryKey(cid);
		
	}

	@Override
	public void delete(Long[] cids) {
		for(Long cid:cids){
			tbCoachMapper.deleteByPrimaryKey(cid);
		}
		

	}

	@Override
	public PageResult findPageCoach(TbCoach coach, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		TbCoachExample example = new TbCoachExample();
		Criteria create = example.createCriteria();
			if(coach!=null){
				if(coach.getName()!=null && coach.getName().length()>0){
					create.andNameLike("%"+coach.getName()+"%");
				}if(coach.getPhone()!=null && coach.getPhone().length()>0){
					create.andPhoneLike("%"+coach.getPhone()+"%");
				}if(coach.getAuthentication()!=null && coach.getAuthentication().length()>0){
					create.andAuthenticationLike("%"+coach.getAuthentication()+"%");
				}if(coach.getStatus()!=null && coach.getStatus().length()>0){
					create.andStatusEqualTo(coach.getStatus());
				}
			}
		
		Page<TbCoach> page = (Page<TbCoach>) tbCoachMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	//更新状态
	@Override
	public void updateAuthentication(Long cid, String authentication) {
		TbCoach coach = tbCoachMapper.selectByPrimaryKey(cid);
		coach.setAuthentication(authentication);
		tbCoachMapper.updateByPrimaryKey(coach);
		
	}

	@Override
	public ResultVO<CoachCommentUser> findCoachDetail(String id) {
		ResultVO<CoachCommentUser> result=new ResultVO<>();
		CoachCommentUser comment= tbCoachMapper.selectCoachCommentUser(id);
		result.setCode(1);
	    result.setMsg("成功获取教练的信息");
	    result.setData(comment);		
		return result;
	}

	//当前驾校的所有科目二的教练，index:0:代表室内模拟练习 1：代表现场练习
	@Override
	public Map<String, Object> findSubject(String subject,String indoor,String status	) {
		
		HashMap<String, Object> result = new HashMap<String, Object>();
		
		TbCoach coach = new TbCoach();
		coach.setSubject(subject);
		coach.setIndoor(indoor);
		coach.setStatus(status);
		
		List<TbCoach> a = tbCoachMapper.selectByCoach(coach);
		
		result.put("Info", a);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
		
		
	}

	//修改教练信息
	@Override
	public Map<String, Object> updateCoach(Map<String, String> params) {
	
		Map<String, Object> result = new HashMap<String,Object>();

		TbCoach coach = new TbCoach();
		coach.setOpenid(params.get("openid"));
		coach.setCid(Long.parseLong(params.get("cid")));
		coach.setSex(params.get("sex"));
		coach.setPhone(params.get("phone"));
		coach.setPlateNumber(params.get("plate_number"));
		coach.setSubject(params.get("subject"));
		coach.setIndoor(params.get("indoor"));
		coach.setNickname(params.get("nickname"));
		coach.setHeadPic(params.get("head_pic"));
		coach.setDriveYear(params.get("drive_year"));
		coach.setCoachYear(params.get("coach_year"));
		coach.setPrecent(params.get("precent"));
		coach.setStar(Integer.parseInt(params.get("star")));
		tbCoachMapper.updateByPrimaryKeySelective(coach);
		
		TbCoach c = tbCoachMapper.selectByPrimaryKey(coach.getCid());
		
		result.put("userinfo", c);
		result.put("statusCode", 200);
		result.put("message", "修改成功");		
		return result;
	}

	//教练登录
	@Override
	public Map<String, Object> login(String openid,String nickname, String head_pic) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		TbCoach coach = new TbCoach();
		coach.setOpenid(openid);		
		List<TbCoach> a = tbCoachMapper.selectByCoach(coach);
		if(a.size() > 0){
			//存在用户
			result.put("userinfo", a.get(0));
			result.put("openid", openid);
			result.put("statusCode", 200);
			result.put("message", "登录成功");	
		}else{
			coach.setHeadPic(head_pic);
			coach.setNickname(nickname);
			coach.setCreated(new Date());
			int res = tbCoachMapper.insertSelective(coach);
			if(res > 0){
				List<TbCoach> b = tbCoachMapper.selectByCoach(coach);
				result.put("userinfo", b.get(0));
				result.put("openid", openid);
				result.put("statusCode", 200);
				result.put("message", "登录成功");	
			}else{
				result.put("statusCode", 500);
				result.put("message", "注册失败");	
			}
		}
			
		return result;
	}
	//提交认证信息
	@Override
	public Map<String, Object> updaAuthentication(Map<String, String> parmes) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		TbCoach coach = new TbCoach();
		coach.setCid(Long.parseLong(parmes.get("cid")));
		coach.setLogoPic(parmes.get("logo_pic"));
		coach.setPhone(parmes.get("phone"));
		coach.setName(parmes.get("name"));
		coach.setCardId(parmes.get("card_id"));
		coach.setSubject(parmes.get("subject"));
		coach.setIndoor(parmes.get("indoor"));
		coach.setVehicleType(parmes.get("vehicle_type"));
		coach.setStatus(parmes.get("status"));
		tbCoachMapper.updateByPrimaryKeySelective(coach);
		
		TbCoach a = tbCoachMapper.selectByPrimaryKey(coach.getCid());
		result.put("userInfo", a);
		result.put("statusCode", 200);
		result.put("message", "等待认证");		
		return result;
	}

	//获取 某个教练 某个日气的学员
	@Override
	public Map<String, Object> mystudent(Long cid, String openid, String status, String date) {
		
		HashMap<String, Object> result = new HashMap<String,Object>();

		ReleConseReserStud a = new ReleConseReserStud();
		a.setDate(date);
		a.setCid(cid);
		List<ReleConseReserStud> c = tbCoachMapper.selectByReleConseReserStud(a);
		
		List<ReleConseReserStud> lst = c.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()-> new TreeSet<>(Comparator.comparing(o-> o.getName()))),ArrayList::new));
		
		
		
		/*List<ReleConseReserStud> b = new ArrayList<>();
		for (ReleConseReserStud releConseReserStud : c) {
			
			if(!b.contains(releConseReserStud))
				b.add(releConseReserStud);
		}*/
		
		result.put("appointments", lst);
		result.put("statusCode", 200);
		result.put("message", "认证成功");	
		return result;
		
	}
		
	//获取工单 已预约 已完成
		@Override
		public Map<String, Object> selectappointment(Long cid, String openid, String status, String checkStatus,String date) {
			HashMap<String, Object> result = new HashMap<String,Object>();

			ReleConseReserStud a = new ReleConseReserStud();
			a.setCheckStatus(checkStatus);
			a.setCid(cid);
			a.setDate(date);
			a.setStatus(status);
			List<ReleConseReserStud> stud = tbCoachMapper.selectByReleConseReserStud(a);
			
			result.put("appointments", stud);
			result.put("statusCode", 200);
			result.put("message", "请求成功");
			return result;
		}
		/*List<TbReservation> list = new ArrayList<>();
		
		TbReservation a = new TbReservation();
		a.setCid(cid);
		a.setStatus(status);
		
		List<Object> arrayList = new ArrayList<Object>();
		
		list = tbReservationMapper.selectByCoachCid(a);
		
			for (TbReservation c : list) {
				TbStudent d = new TbStudent();
				
				TbStudent b = tbStudentMapper.selectByPrimaryKey(c.getSid());
				d.setNickName(b.getNickName());
				d.setPhone(b.getPhone());
				d.setHeadPic(b.getHeadPic());
				d.setName(b.getName());
				
				arrayList.add(d);
				arrayList.add(c.getCheckStatus());
			}
		
			
			List<TbReleaseNews> array = new ArrayList<TbReleaseNews>();
			TbReleaseNews example = new TbReleaseNews();
			example.setCid(cid);		
			example.setDate(date);
			array =(List<TbReleaseNews>) tbReleaseNewsMapper.selectByExampleId(example);
		
		result.put("appointments", arrayList);
		result.put("date", date);	
		result.put("statusCode", 200);
		result.put("message", "请求成功");		
		return result;*/

		//修改状态 确定为已完成 未完成
		@Override
		public Map<String, Object> updateCheckStatus(Long reservationId, String status, String checkStatus, String openid) {
			
			HashMap<String, Object> result = new HashMap<String,Object>();
			
			TbReservation res = new TbReservation();
			res.setId(reservationId);
			res.setCheckStatus(checkStatus);
			res.setStatus(status);
			tbReservationMapper.updateByPrimaryKeySelective(res);
			result.put("statusCode", 200);
			result.put("message", "请求成功");		
			return result;
		}
	

	


	

	
}
